package edu.ucdavis.gwt.gis.client.toolbar.button;

import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

import edu.ucdavis.gwt.gis.client.toolbar.Toolbar;

public abstract class ToolbarItem extends Composite implements ClickHandler {
	
	private HorizontalPanel panel = new HorizontalPanel();
	private FocusPanel eventPanel = null;
	
	public void init() {
		eventPanel = new FocusPanel();
		panel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		
		Image icon = getIcon();		
		if( icon != null ) {
			icon.getElement().getStyle().setVerticalAlign(VerticalAlign.TOP);
			panel.add(icon);
		}	
		
		String txt = getText();
		if( !txt.isEmpty() ) txt = "&nbsp;&nbsp;"+txt;
		panel.add(new HTML(txt));
	
		
		eventPanel.add(panel);
		eventPanel.getElement().getStyle().setProperty("outline", "none");
		eventPanel.addClickHandler(this);
		panel.setStyleName("toolbarButton");
		initWidget(eventPanel);
	}
	
	public abstract Image getIcon();
	
	public abstract String getText();
	
	public abstract void onAdd(Toolbar toolbar);
	
	public void addClickHandler(ClickHandler handler) {
		eventPanel.addClickHandler(handler);
	}
	
}
